import bean.QueryParam;
import bean.Student;
import dao.StudentDao;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import utils.MyBatisUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 飞哥
 * @version V1.0
 * @package PACKAGE_NAME
 * @date 2021/7/11 10:23
 */
public class MyBatisTest01 {

    /**
     * @date 2021年7月11日10:29:08
     * 使用mybatis动态代理机制，使用sqlSession.getMapper(dao接口)
     * getMapper能够获取dao接口对应的实现类对象
     */
    @Test
    public void selectStudents() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        // getMapper能够获取dao接口对应的实现类对象
        StudentDao dao = sqlSession.getMapper(StudentDao.class);

        /**
         * com.sun.proxy.$Proxy6
         * jdk的动态代理
         */
        System.out.println(dao.getClass().getName());


        // 调用dao的方法，执行数据库的操作
        List<Student> list = dao.queryStudents();
        for (Student student : list) {
            System.out.println(student);
        }
        sqlSession.close();
    }

    @Test
    public void insertStudent() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        StudentDao dao = sqlSession.getMapper(StudentDao.class);
        Student student = new Student(1004, "孙一宁", "sunyining@163.com", 24);
        int insertStudent = dao.insertStudent(student);
        sqlSession.commit();
        System.out.println("成功添加" + insertStudent + "条数据");

        sqlSession.close();
    }

    @Test
    public void selectStudentById() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        StudentDao dao = sqlSession.getMapper(StudentDao.class);
        Student student = dao.queryStudentById(1002);
        System.out.println(student);
        sqlSession.close();
    }

    @Test
    public void selectStudentByNameUse$(){
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        StudentDao dao = sqlSession.getMapper(StudentDao.class);
        //因为$采用的是字符串连接的方式，所以双引号里面要加一个单引号
        Student student = dao.selectStudentByNameUse$("'代心如'");
        System.out.println(student);
        //关闭sqlSession对象
        sqlSession.close();
    }

    @Test
    public void selectStudentByNameUse$Order(){
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        StudentDao dao = sqlSession.getMapper(StudentDao.class);
        List<Student> list = dao.selectStudentByNameUse$Order("name");//想要更换排序方式直接在这里更改
        for (Student student : list) {
            System.out.println(student);
        }
        //关闭sqlSession对象
        sqlSession.close();
    }

    @Test
    public void selectMultiParam() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
        List<Student> list = studentDao.selectMultiParam("代心如", 21);
        for (Student student : list) {
            System.out.println("姓名为代心如或者年龄是21的学生信息" + student);
        }
    }

    @Test
    public void selectMultiObject() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
        QueryParam queryParam = new QueryParam("代心如", 21);
        List<Student> list = studentDao.selectMultiObject(queryParam);
        for (Student student : list) {
            System.out.println("姓名为代心如或者年龄是21的学生信息" + student);
        }
    }

    @Test
    public void selectMultiPosition() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
        List<Student> list = studentDao.selectMultiPosition("代心如", 21);
        for (Student student : list) {
            System.out.println("姓名为代心如或者年龄是21的学生信息" + student);
        }
    }

    @Test
    public void selectMultiByMap() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("myName","代心如");
        map.put("myAge",21);
        List<Student> list = studentDao.selectMultiByMap(map);
        for (Student student : list) {
            System.out.println("姓名为代心如或者年龄是21的学生信息" + student);
        }
    }
}
/**
 * # 和 $区别(*****非常重要*****)
 * 	  1. #使用 ？在sql语句中做占位的， 使用PreparedStatement执行sql，效率高
 * 	  2. #能够避免sql注入，更安全。
 * 	  3. $不使用占位符，是字符串连接方式，使用Statement对象执行sql，效率低
 * 	  4. $有sql注入的风险，缺乏安全性。
 * 	  5. $可以替换表名或者列名
 */
